home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / setup.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  21.7 KB  |  702 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '8.0'
  5. __title__ = 'Printer/Fax Setup Utility'
  6. __mod__ = 'hp-setup'
  7. __doc__ = 'Installs HPLIP printers and faxes in the CUPS spooler. Tries to automatically determine the correct PPD file to use. Allows the printing of a testpage. Performs basic fax parameter setup.'
  8. import sys
  9. import getopt
  10. import time
  11. import os.path as os
  12. import re
  13. import os
  14. import gzip
  15.  
  16. try:
  17.     import readline
  18. except ImportError:
  19.     pass
  20.  
  21. from base.g import *
  22. from base import device, utils, tui, models, module
  23. from prnt import cups
  24. pm = None
  25.  
  26. def plugin_download_callback(c, s, t):
  27.     pm.update(int(100 * c * s / t), utils.format_bytes(c * s))
  28.  
  29. nickname_pat = re.compile('\\*NickName:\\s*\\"(.*)"', re.MULTILINE)
  30. USAGE = [
  31.     (__doc__, '', 'name', True),
  32.     ('Usage: %s [MODE] [OPTIONS] [SERIAL NO.|USB bus:device|IP|DEVNODE]' % __mod__, '', 'summary', True),
  33.     utils.USAGE_MODE,
  34.     utils.USAGE_GUI_MODE,
  35.     utils.USAGE_INTERACTIVE_MODE,
  36.     utils.USAGE_SPACE,
  37.     utils.USAGE_OPTIONS,
  38.     ('Automatic mode:', '-a or --auto (-i mode only)', 'option', False),
  39.     ('To specify the port on a multi-port JetDirect:', '--port=<port> (Valid values are 1\\*, 2, and 3. \\*default)', 'option', False),
  40.     ('No testpage in automatic mode:', '-x (-i mode only)', 'option', False),
  41.     ('To specify a CUPS printer queue name:', '-p<printer> or --printer=<printer> (-i mode only)', 'option', False),
  42.     ('To specify a CUPS fax queue name:', '-f<fax> or --fax=<fax> (-i mode only)', 'option', False),
  43.     ('Type of queue(s) to install:', '-t<typelist> or --type=<typelist>. <typelist>: print*, fax\\* (\\*default) (-i mode only)', 'option', False),
  44.     ('To specify the device URI to install:', '-d<device> or --device=<device> (--qt4 mode only)', 'option', False),
  45.     utils.USAGE_LANGUAGE,
  46.     utils.USAGE_LOGGING1,
  47.     utils.USAGE_LOGGING2,
  48.     utils.USAGE_LOGGING3,
  49.     utils.USAGE_HELP,
  50.     ('[SERIAL NO.|USB ID|IP|DEVNODE]', '', 'heading', False),
  51.     ('USB bus:device (usb only):', '"xxx:yyy" where \'xxx\' is the USB bus and \'yyy\' is the USB device. (Note: The \':\' and all leading zeros must be present.)', 'option', False),
  52.     ('', "Use the 'lsusb' command to obtain this information.", 'option', False),
  53.     ('IPs (network only):', 'IPv4 address "a.b.c.d" or "hostname"', 'option', False),
  54.     ('DEVNODE (parallel only):', '"/dev/parportX", X=0,1,2,...', 'option', False),
  55.     ('SERIAL NO. (usb and parallel only):', '"serial no."', 'option', True),
  56.     utils.USAGE_EXAMPLES,
  57.     ('Setup using GUI mode:', '$ hp-setup', 'example', False),
  58.     ('Setup using GUI mode, specifying usb:', '$ hp-setup -b usb', 'example', False),
  59.     ('Setup using GUI mode, specifying an IP:', '$ hp-setup 192.168.0.101', 'example', False),
  60.     ('One USB printer attached, automatic:', '$ hp-setup -i -a', 'example', False),
  61.     ('USB, IDs specified:', '$ hp-setup -i 001:002', 'example', False),
  62.     ('Network:', '$ hp-setup -i 66.35.250.209', 'example', False),
  63.     ('Network, Jetdirect port 2:', '$ hp-setup -i --port=2 66.35.250.209', 'example', False),
  64.     ('Parallel:', '$ hp-setup -i /dev/parport0', 'example', False),
  65.     ('USB or parallel, using serial number:', '$ hp-setup -i US12345678A', 'example', False),
  66.     ('USB, automatic:', '$ hp-setup -i --auto 001:002', 'example', False),
  67.     ('Parallel, automatic, no testpage:', '$ hp-setup -i -a -x /dev/parport0', 'example', False),
  68.     ('Parallel, choose device:', '$ hp-setup -i -b par', 'example', False),
  69.     utils.USAGE_SPACE,
  70.     utils.USAGE_NOTES,
  71.     ('1. If no serial number, USB ID, IP, or device node is specified, the USB and parallel busses will be probed for devices.', '', 'note', False),
  72.     ("2. Using 'lsusb' to obtain USB IDs: (example)", '', 'note', False),
  73.     ('   $ lsusb', '', 'note', False),
  74.     ('         Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard', '', 'note', False),
  75.     ('   $ hp-setup --auto 003:011', '', 'note', False),
  76.     ("   (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", '', 'note', True),
  77.     ('3. Parameters -a, -f, -p, or -t are not valid in GUI (-u) mode.', '', 'note', True),
  78.     utils.USAGE_SPACE,
  79.     utils.USAGE_SEEALSO,
  80.     ('hp-makeuri', '', 'seealso', False),
  81.     ('hp-probe', '', 'seealso', False)]
  82.  
  83. def restart_cups():
  84.     if os.path.exists('/etc/init.d/cups'):
  85.         return '/etc/init.d/cups restart'
  86.     if os.path.exists('/etc/init.d/cupsys'):
  87.         return '/etc/init.d/cupsys restart'
  88.     return 'killall -HUP cupsd'
  89.  
  90. mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE, (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4), run_as_root_ok = True)
  91. (opts, device_uri, printer_name, mode, ui_toolkit, loc) = mod.parseStdOpts('axp:P:f:t:b:d:', [
  92.     'ttl=',
  93.     'filter=',
  94.     'search=',
  95.     'find=',
  96.     'method=',
  97.     'time-out=',
  98.     'timeout=',
  99.     'printer=',
  100.     'fax=',
  101.     'type=',
  102.     'port=',
  103.     'auto',
  104.     'device='], handle_device_printer = False)
  105. printer_name = None
  106. fax_name = None
  107. bus = None
  108. setup_print = True
  109. setup_fax = True
  110. makeuri = None
  111. auto = False
  112. testpage_in_auto_mode = True
  113. jd_port = 1
  114. for o, a in opts:
  115.     if o == '-x':
  116.         testpage_in_auto_mode = False
  117.         continue
  118.     if o in ('-P', '-p', '--printer'):
  119.         printer_name = a
  120.         continue
  121.     if o in ('-f', '--fax'):
  122.         fax_name = a
  123.         continue
  124.     if o in ('-d', '--device'):
  125.         device_uri = a
  126.         continue
  127.     if o in ('-b', '--bus'):
  128.         bus = [ x.lower().strip() for x in a.split(',') ]
  129.         if not device.validateBusList(bus, False):
  130.             mod.usage(error_msg = [
  131.                 'Invalid bus name'])
  132.         
  133.     device.validateBusList(bus, False)
  134.     if o in ('-t', '--type'):
  135.         setup_fax = False
  136.         setup_print = False
  137.         a = a.strip().lower()
  138.         for aa in a.split(','):
  139.             if aa.strip() == 'print':
  140.                 setup_print = True
  141.                 continue
  142.             None if aa.strip() not in ('print', 'fax') else []
  143.             if aa.strip() == 'fax':
  144.                 if not prop.fax_build:
  145.                     log.error('Cannot enable fax setup - HPLIP not built with fax enabled.')
  146.                 else:
  147.                     setup_fax = True
  148.             prop.fax_build
  149.         
  150.     if o == '--port':
  151.         
  152.         try:
  153.             jd_port = int(a)
  154.         except ValueError:
  155.             mod.usage(error_msg = [
  156.                 'Invalid port number. Must be between 1 and 3 inclusive.'])
  157.         except:
  158.             None<EXCEPTION MATCH>ValueError
  159.         
  160.  
  161.     None<EXCEPTION MATCH>ValueError
  162.     if o in ('-a', '--auto'):
  163.         auto = True
  164.         continue
  165.  
  166.  
  167. try:
  168.     param = mod.args[0]
  169. except IndexError:
  170.     param = ''
  171.  
  172. log.debug('param=%s' % param)
  173. if mode == GUI_MODE:
  174.     if ui_toolkit == 'qt3':
  175.         if not utils.canEnterGUIMode():
  176.             log.error('%s requires GUI support (try running with --qt4). Also, try using interactive (-i) mode.' % __mod__)
  177.             sys.exit(1)
  178.         
  179.     elif not utils.canEnterGUIMode4():
  180.         log.error('%s requires GUI support (try running with --qt3). Also, try using interactive (-i) mode.' % __mod__)
  181.         sys.exit(1)
  182.     
  183.  
  184. if mode == GUI_MODE:
  185.     pass
  186. None if ui_toolkit == 'qt3' else None<EXCEPTION MATCH>KeyboardInterrupt
  187.  
  188. try:
  189.     if not os.geteuid() == 0:
  190.         log.error('You must be root to run this utility.')
  191.         sys.exit(1)
  192.     
  193.     if not auto:
  194.         log.info("(Note: Defaults for each question are maked with a '*'. Press <enter> to accept the default.)")
  195.         log.info('')
  196.     
  197.     if param:
  198.         (device_uri, sane_uri, fax_uri) = device.makeURI(param, jd_port)
  199.     
  200.     if not device_uri and bus is None:
  201.         bus = tui.connection_table()
  202.         if bus is None:
  203.             sys.exit(0)
  204.         
  205.         log.info('\nUsing connection type: %s' % bus[0])
  206.         log.info('')
  207.     
  208.     if not device_uri:
  209.         device_uri = mod.getDeviceUri(device_uri, devices = device.probeDevices(bus))
  210.     
  211.     log.info(log.bold('\nSetting up device: %s\n' % device_uri))
  212.     log.info('')
  213.     print_uri = device_uri.replace('hpfax:', 'hp:')
  214.     fax_uri = device_uri.replace('hp:', 'hpfax:')
  215.     (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(device_uri)
  216.     log.debug('Model=%s' % model)
  217.     mq = device.queryModelByURI(device_uri)
  218.     if not mq or mq.get('support-type', SUPPORT_TYPE_NONE) == SUPPORT_TYPE_NONE:
  219.         log.error('Unsupported printer model.')
  220.         sys.exit(1)
  221.     
  222.     if mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_NONE, FAX_TYPE_NOT_SUPPORTED) and setup_fax:
  223.         setup_fax = False
  224.     
  225.     norm_model = models.normalizeModelName(model).lower()
  226.     plugin = mq.get('plugin', PLUGIN_NONE)
  227.     plugin_installed = utils.to_bool(sys_state.get('plugin', 'installed', '0'))
  228.     if plugin > PLUGIN_NONE and not plugin_installed:
  229.         tui.header('PLUG-IN INSTALLATION')
  230.         hp_plugin = utils.which('hp-plugin')
  231.         if hp_plugin:
  232.             if prop.gui_build:
  233.                 os.system('hp-plugin -i')
  234.             else:
  235.                 os.system('hp-plugin')
  236.         
  237.     
  238.     ppds = cups.getSystemPPDs()
  239.     default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  240.     stripped_model = cups.stripModel2(default_model)
  241.     installed_print_devices = device.getSupportedCUPSDevices([
  242.         'hp'])
  243.     for d in installed_print_devices.keys():
  244.         for p in installed_print_devices[d]:
  245.             log.debug("found print queue '%s'" % p)
  246.         
  247.     
  248.     installed_fax_devices = device.getSupportedCUPSDevices([
  249.         'hpfax'])
  250.     for d in installed_fax_devices.keys():
  251.         for f in installed_fax_devices[d]:
  252.             log.debug("found fax queue '%s'" % f)
  253.         
  254.     
  255.     if setup_print:
  256.         tui.header('PRINT QUEUE SETUP')
  257.         if not auto and print_uri in installed_print_devices:
  258.             log.warning('One or more print queues already exist for this device: %s.' % ', '.join(installed_print_devices[print_uri]))
  259.             (ok, setup_print) = tui.enter_yes_no('\nWould you like to install another print queue for this device', 'n')
  260.             if not ok:
  261.                 sys.exit(0)
  262.             
  263.         
  264.     
  265.     if setup_print:
  266.         if auto:
  267.             printer_name = default_model
  268.         
  269.         printer_default_model = default_model
  270.         if device_uri in installed_print_devices and printer_default_model in installed_print_devices[device_uri]:
  271.             i = 2
  272.             while True:
  273.                 t = printer_default_model + '_%d' % i
  274.                 if t not in installed_print_devices[device_uri]:
  275.                     printer_default_model += '_%d' % i
  276.                     break
  277.                 
  278.                 i += 1
  279.         
  280.         if not auto:
  281.             if printer_name is None:
  282.                 while True:
  283.                     printer_name = raw_input(log.bold("\nPlease enter a name for this print queue (m=use model name:'%s'*, q=quit) ?" % printer_default_model))
  284.                     if printer_name.lower().strip() == 'q':
  285.                         log.info('OK, done.')
  286.                         sys.exit(0)
  287.                     
  288.                     if not printer_name or printer_name.lower().strip() == 'm':
  289.                         printer_name = printer_default_model
  290.                     
  291.                     name_ok = True
  292.                     for d in installed_print_devices.keys():
  293.                         for p in installed_print_devices[d]:
  294.                             if printer_name == p:
  295.                                 log.error('A print queue with that name already exists. Please enter a different name.')
  296.                                 name_ok = False
  297.                                 break
  298.                                 continue
  299.                         
  300.                     
  301.                     for d in installed_fax_devices.keys():
  302.                         for f in installed_fax_devices[d]:
  303.                             if printer_name == f:
  304.                                 log.error('A fax queue with that name already exists. Please enter a different name.')
  305.                                 name_ok = False
  306.                                 break
  307.                                 continue
  308.                         
  309.                     
  310.                     for c in printer_name:
  311.                         if c in (' ', '#', '/', '%'):
  312.                             log.error("Invalid character '%s' in printer name. Please enter a name that does not contain this character." % c)
  313.                             name_ok = False
  314.                             continue
  315.                     
  316.                     if name_ok:
  317.                         break
  318.                         continue
  319.             
  320.         else:
  321.             printer_name = printer_default_model
  322.         log.info('Using queue name: %s' % printer_name)
  323.         default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  324.         stripped_model = default_model.lower().replace('hp-', '').replace('hp_', '')
  325.         log.info('Locating PPD file... Please wait.')
  326.         print_ppd = cups.getPPDFile2(stripped_model, ppds)
  327.         enter_ppd = False
  328.         if print_ppd is None:
  329.             enter_ppd = True
  330.             log.error('Unable to find an appropriate PPD file.')
  331.         else:
  332.             (print_ppd, desc) = print_ppd
  333.             log.info('\nFound PPD file: %s' % print_ppd)
  334.             if desc:
  335.                 log.info('Description: %s' % desc)
  336.                 if not auto:
  337.                     log.info('\nNote: The model number may vary slightly from the actual model number on the device.')
  338.                     (ok, ans) = tui.enter_yes_no('\nDoes this PPD file appear to be the correct one')
  339.                     if not ok:
  340.                         sys.exit(0)
  341.                     
  342.                     if not ans:
  343.                         enter_ppd = True
  344.                     
  345.                 
  346.             
  347.         if enter_ppd:
  348.             enter_ppd = False
  349.             (ok, enter_ppd) = tui.enter_yes_no('\nWould you like to specify the path to the correct PPD file to use', 'n')
  350.             if not ok:
  351.                 sys.exit(0)
  352.             
  353.             if enter_ppd:
  354.                 ok = False
  355.                 while True:
  356.                     user_input = raw_input(log.bold('\nPlease enter the full filesystem path to the PPD file to use (q=quit) :'))
  357.                     if user_input.lower().strip() == 'q':
  358.                         log.info('OK, done.')
  359.                         sys.exit(0)
  360.                     
  361.                     file_path = user_input
  362.                     if os.path.exists(file_path) and os.path.isfile(file_path):
  363.                         if file_path.endswith('.gz'):
  364.                             nickname = gzip.GzipFile(file_path, 'r').read(4096)
  365.                         else:
  366.                             nickname = file(file_path, 'r').read(4096)
  367.                         
  368.                         try:
  369.                             desc = nickname_pat.search(nickname).group(1)
  370.                         except AttributeError:
  371.                             desc = ''
  372.  
  373.                         if desc:
  374.                             log.info('Description for the file: %s' % desc)
  375.                         else:
  376.                             log.error("No PPD 'NickName' found. This file may not be a valid PPD file.")
  377.                         (ok, ans) = tui.enter_yes_no('\nUse this file')
  378.                         if not ok:
  379.                             sys.exit(0)
  380.                         
  381.                         if ans:
  382.                             print_ppd = file_path
  383.                         
  384.                     else:
  385.                         log.error('File not found or not an appropriate (PPD) file.')
  386.                     if ok:
  387.                         break
  388.                         continue
  389.             else:
  390.                 log.error('PPD file required. Setup cannot continue. Exiting.')
  391.                 sys.exit(1)
  392.         
  393.         if auto:
  394.             (location, info) = ('', 'Automatically setup by HPLIP')
  395.         else:
  396.             while True:
  397.                 location = raw_input(log.bold('Enter a location description for this printer (q=quit) ?'))
  398.                 if location.strip().lower() == 'q':
  399.                     log.info('OK, done.')
  400.                     sys.exit(0)
  401.                 
  402.                 break
  403.             while True:
  404.                 info = raw_input(log.bold('Enter additonal information or notes for this printer (q=quit) ?'))
  405.                 if info.strip().lower() == 'q':
  406.                     log.info('OK, done.')
  407.                     sys.exit(0)
  408.                 
  409.                 break
  410.         log.info(log.bold('\nAdding print queue to CUPS:'))
  411.         log.info('Device URI: %s' % print_uri)
  412.         log.info('Queue name: %s' % printer_name)
  413.         log.info('PPD file: %s' % print_ppd)
  414.         log.info('Location: %s' % location)
  415.         log.info('Information: %s' % info)
  416.         log.debug('Restarting CUPS...')
  417.         (status, output) = utils.run(restart_cups())
  418.         log.debug('Restart CUPS returned: exit=%d output=%s' % (status, output))
  419.         if not os.path.exists(print_ppd):
  420.             (status, status_str) = cups.addPrinter(printer_name.encode('utf8'), print_uri, location, '', print_ppd, info)
  421.         else:
  422.             (status, status_str) = cups.addPrinter(printer_name.encode('utf8'), print_uri, location, print_ppd, '', info)
  423.         log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  424.         installed_print_devices = device.getSupportedCUPSDevices([
  425.             'hp'])
  426.         if print_uri not in installed_print_devices or printer_name not in installed_print_devices[print_uri]:
  427.             log.error('Printer queue setup failed. Please restart CUPS and try again.')
  428.             sys.exit(1)
  429.         
  430.     
  431.     if setup_fax and not (prop.fax_build):
  432.         log.error('Cannot setup fax - HPLIP not built with fax enabled.')
  433.         setup_fax = False
  434.     
  435.     if setup_fax:
  436.         
  437.         try:
  438.             from fax import fax
  439.         except ImportError:
  440.             setup_fax = False
  441.             log.warning('Fax setup disabled - Python 2.3+ required.')
  442.         except:
  443.             None<EXCEPTION MATCH>ImportError
  444.         
  445.  
  446.     None<EXCEPTION MATCH>ImportError
  447.     log.info('')
  448.     if setup_fax:
  449.         tui.header('FAX QUEUE SETUP')
  450.         if not auto and fax_uri in installed_fax_devices:
  451.             log.warning('One or more fax queues already exist for this device: %s.' % ', '.join(installed_fax_devices[fax_uri]))
  452.             (ok, setup_fax) = tui.enter_yes_no('\nWould you like to install another fax queue for this device', 'n')
  453.             if not ok:
  454.                 sys.exit(0)
  455.             
  456.         
  457.     
  458.     if setup_fax:
  459.         if auto:
  460.             fax_name = default_model + '_fax'
  461.         
  462.         fax_default_model = default_model + '_fax'
  463.         if fax_uri in installed_fax_devices and fax_default_model in installed_fax_devices[fax_uri]:
  464.             i = 2
  465.             while True:
  466.                 t = fax_default_model + '_%d' % i
  467.                 if t not in installed_fax_devices[fax_uri]:
  468.                     fax_default_model += '_%d' % i
  469.                     break
  470.                 
  471.                 i += 1
  472.         
  473.         if not auto:
  474.             if fax_name is None:
  475.                 while True:
  476.                     fax_name = raw_input(log.bold("\nPlease enter a name for this fax queue (m=use model name:'%s'*, q=quit) ?" % fax_default_model))
  477.                     if fax_name.lower().strip() == 'q':
  478.                         log.info('OK, done.')
  479.                         sys.exit(0)
  480.                     
  481.                     if not fax_name or fax_name.lower().strip() == 'm':
  482.                         fax_name = fax_default_model
  483.                     
  484.                     name_ok = True
  485.                     for d in installed_print_devices.keys():
  486.                         for p in installed_print_devices[d]:
  487.                             if fax_name == p:
  488.                                 log.error('A print queue with that name already exists. Please enter a different name.')
  489.                                 name_ok = False
  490.                                 break
  491.                                 continue
  492.                         
  493.                     
  494.                     for d in installed_fax_devices.keys():
  495.                         for f in installed_fax_devices[d]:
  496.                             if fax_name == f:
  497.                                 log.error('A fax queue with that name already exists. Please enter a different name.')
  498.                                 name_ok = False
  499.                                 break
  500.                                 continue
  501.                         
  502.                     
  503.                     for c in fax_name:
  504.                         if c in (' ', '#', '/', '%'):
  505.                             log.error("Invalid character '%s' in fax name. Please enter a name that does not contain this character." % c)
  506.                             name_ok = False
  507.                             continue
  508.                     
  509.                     if name_ok:
  510.                         break
  511.                         continue
  512.             
  513.         else:
  514.             fax_name = fax_default_model
  515.         log.info('Using queue name: %s' % fax_name)
  516.         fax_type = mq.get('fax-type', FAX_TYPE_NONE)
  517.         if fax_type == FAX_TYPE_SOAP:
  518.             fax_ppd_name = 'HP-Fax2-hplip'
  519.         else:
  520.             fax_ppd_name = 'HP-Fax-hplip'
  521.         for f in ppds:
  522.             if f.find(fax_ppd_name) >= 0:
  523.                 fax_ppd = f
  524.                 log.debug('Found PDD file: %s' % fax_ppd)
  525.                 break
  526.                 continue
  527.         else:
  528.             sys.exit(1)
  529.         if auto:
  530.             (location, info) = ('', 'Automatically setup by HPLIP')
  531.         else:
  532.             while True:
  533.                 location = raw_input(log.bold('Enter a location description for this printer (q=quit) ?'))
  534.                 if location.strip().lower() == 'q':
  535.                     log.info('OK, done.')
  536.                     sys.exit(0)
  537.                 
  538.                 break
  539.             while True:
  540.                 info = raw_input(log.bold('Enter additonal information or notes for this printer (q=quit) ?'))
  541.                 if info.strip().lower() == 'q':
  542.                     log.info('OK, done.')
  543.                     sys.exit(0)
  544.                 
  545.                 break
  546.         log.info(log.bold('\nAdding fax queue to CUPS:'))
  547.         log.info('Device URI: %s' % fax_uri)
  548.         log.info('Queue name: %s' % fax_name)
  549.         log.info('PPD file: %s' % fax_ppd)
  550.         log.info('Location: %s' % location)
  551.         log.info('Information: %s' % info)
  552.         if not os.path.exists(fax_ppd):
  553.             (status, status_str) = cups.addPrinter(fax_name.encode('utf8'), fax_uri, location, '', fax_ppd, info)
  554.         else:
  555.             (status, status_str) = cups.addPrinter(fax_name.encode('utf8'), fax_uri, location, fax_ppd, '', info)
  556.         log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  557.         installed_fax_devices = device.getSupportedCUPSDevices([
  558.             'hpfax'])
  559.         log.debug(installed_fax_devices)
  560.         if fax_uri not in installed_fax_devices or fax_name not in installed_fax_devices[fax_uri]:
  561.             log.error('Fax queue setup failed. Please restart CUPS and try again.')
  562.             sys.exit(1)
  563.         
  564.         tui.header('FAX HEADER SETUP')
  565.         if auto:
  566.             setup_fax = False
  567.         else:
  568.             while True:
  569.                 user_input = raw_input(log.bold('\nWould you like to perform fax header setup (y=yes*, n=no, q=quit) ?')).strip().lower()
  570.                 if user_input == 'q':
  571.                     log.info('OK, done.')
  572.                     sys.exit(0)
  573.                 
  574.                 if not user_input:
  575.                     user_input = 'y'
  576.                 
  577.                 setup_fax = user_input == 'y'
  578.                 if user_input in ('y', 'n', 'q'):
  579.                     break
  580.                 
  581.                 log.error("Please enter 'y' or 'n'")
  582.         if setup_fax:
  583.             d = fax.getFaxDevice(fax_uri, disable_dbus = True)
  584.             
  585.             try:
  586.                 d.open()
  587.             except Error:
  588.                 log.error('Unable to communicate with the device. Please check the device and try again.')
  589.  
  590.             
  591.             try:
  592.                 tries = 0
  593.                 ok = True
  594.                 while True:
  595.                     tries += 1
  596.                     
  597.                     try:
  598.                         current_phone_num = str(d.getPhoneNum())
  599.                         current_station_name = str(d.getStationName())
  600.                     except Error:
  601.                         log.error('Could not communicate with device. Device may be busy. Please wait for retry...')
  602.                         time.sleep(5)
  603.                         ok = False
  604.                         if tries > 12:
  605.                             break
  606.                         
  607.                         tries > 12
  608.  
  609.                     ok = True
  610.                     break
  611.                 if ok:
  612.                     while True:
  613.                         if current_phone_num:
  614.                             phone_num = raw_input(log.bold("\nEnter the fax phone number for this device (c=use current:'%s'*, q=quit) ?" % current_phone_num))
  615.                         else:
  616.                             phone_num = raw_input(log.bold('\nEnter the fax phone number for this device (q=quit) ?'))
  617.                         if phone_num.strip().lower() == 'q':
  618.                             log.info('OK, done.')
  619.                             sys.exit(0)
  620.                         
  621.                         if current_phone_num:
  622.                             if not phone_num or phone_num.strip().lower() == 'c':
  623.                                 phone_num = current_phone_num
  624.                             
  625.                         if len(phone_num) > 50:
  626.                             log.error('Phone number length is too long (>50 characters). Please enter a shorter number.')
  627.                             continue
  628.                         
  629.                         ok = True
  630.                         for x in phone_num:
  631.                             if x not in '0123456789-(+) ':
  632.                                 log.error('Invalid characters in phone number. Please only use 0-9, -, (, +, and )')
  633.                                 ok = False
  634.                                 break
  635.                                 continue
  636.                         
  637.                         if not ok:
  638.                             continue
  639.                         
  640.                         break
  641.                     while True:
  642.                         if current_station_name:
  643.                             station_name = raw_input(log.bold("\nEnter the name and/or company for this device (c=use current:'%s'*, q=quit) ?" % current_station_name))
  644.                         else:
  645.                             station_name = raw_input(log.bold('\nEnter the name and/or company for this device (q=quit) ?'))
  646.                         if station_name.strip().lower() == 'q':
  647.                             log.info('OK, done.')
  648.                             sys.exit(0)
  649.                         
  650.                         if current_station_name:
  651.                             if not station_name or station_name.strip().lower() == 'c':
  652.                                 station_name = current_station_name
  653.                             
  654.                         if len(station_name) > 50:
  655.                             log.error('Name/company length is too long (>50 characters). Please enter a shorter name/company.')
  656.                             continue
  657.                         
  658.                         break
  659.                     
  660.                     try:
  661.                         d.setStationName(station_name)
  662.                         d.setPhoneNum(phone_num)
  663.                     except Error:
  664.                         log.error('Could not communicate with device. Device may be busy.')
  665.  
  666.                     log.info('\nParameters sent to device.')
  667.             finally:
  668.                 d.close()
  669.  
  670.         
  671.     
  672.     if setup_print:
  673.         print_test_page = False
  674.         tui.header('PRINTER TEST PAGE')
  675.         if auto:
  676.             if testpage_in_auto_mode:
  677.                 print_test_page = True
  678.             
  679.         else:
  680.             (ok, print_test_page) = tui.enter_yes_no('\nWould you like to print a test page')
  681.             if not ok:
  682.                 sys.exit(0)
  683.             
  684.         if print_test_page:
  685.             path = utils.which('hp-testpage')
  686.             if printer_name:
  687.                 param = '-p%s' % printer_name
  688.             else:
  689.                 param = '-d%s' % print_uri
  690.             if len(path) > 0:
  691.                 cmd = 'hp-testpage %s' % param
  692.             else:
  693.                 cmd = 'python ./testpage.py %s' % param
  694.             log.debug(cmd)
  695.             os.system(cmd)
  696.         
  697. except KeyboardInterrupt:
  698.     log.error('User exit')
  699.  
  700. log.info('')
  701. log.info('Done.')
  702.